package com.investment.advisor.controller;

import com.investment.advisor.dto.StrategyDTO;
import com.investment.advisor.service.StrategyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/strategies")
@Api(tags = "策略管理", description = "策略的增删改查")
public class StrategyController {

    private final StrategyService strategyService;

    @Autowired
    public StrategyController(StrategyService strategyService) {
        this.strategyService = strategyService;
    }

    @GetMapping
    @ApiOperation("获取所有策略")
    public ResponseEntity<List<StrategyDTO>> getAllStrategies() {
        return ResponseEntity.ok(strategyService.getAllStrategies());
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取策略")
    public ResponseEntity<StrategyDTO> getStrategyById(
            @ApiParam(value = "策略ID", required = true)
            @PathVariable Long id) {
        return ResponseEntity.ok(strategyService.getStrategyById(id));
    }

    @GetMapping("/search")
    @ApiOperation("搜索策略")
    public ResponseEntity<List<StrategyDTO>> searchStrategies(
            @ApiParam(value = "搜索关键词", required = true)
            @RequestParam String keyword) {
        return ResponseEntity.ok(strategyService.searchStrategies(keyword));
    }

    @GetMapping("/factor/{factorId}")
    @ApiOperation("根据因子ID获取相关策略")
    public ResponseEntity<List<StrategyDTO>> getStrategiesByFactorId(
            @ApiParam(value = "因子ID", required = true)
            @PathVariable Long factorId) {
        return ResponseEntity.ok(strategyService.getStrategiesByFactorId(factorId));
    }

    @PostMapping
    @ApiOperation("创建策略")
    public ResponseEntity<StrategyDTO> createStrategy(
            @ApiParam(value = "策略信息", required = true)
            @RequestBody StrategyDTO strategyDTO) {
        return new ResponseEntity<>(strategyService.createStrategy(strategyDTO), HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新策略")
    public ResponseEntity<StrategyDTO> updateStrategy(
            @ApiParam(value = "策略ID", required = true)
            @PathVariable Long id,
            @ApiParam(value = "策略信息", required = true)
            @RequestBody StrategyDTO strategyDTO) {
        return ResponseEntity.ok(strategyService.updateStrategy(id, strategyDTO));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除策略")
    public ResponseEntity<Void> deleteStrategy(
            @ApiParam(value = "策略ID", required = true)
            @PathVariable Long id) {
        strategyService.deleteStrategy(id);
        return ResponseEntity.noContent().build();
    }

    @PostMapping("/{strategyId}/factors/{factorId}")
    @ApiOperation("为策略添加因子")
    public ResponseEntity<Void> addFactorToStrategy(
            @ApiParam(value = "策略ID", required = true)
            @PathVariable Long strategyId,
            @ApiParam(value = "因子ID", required = true)
            @PathVariable Long factorId) {
        strategyService.addFactorToStrategy(strategyId, factorId);
        return ResponseEntity.noContent().build();
    }

    @DeleteMapping("/{strategyId}/factors/{factorId}")
    @ApiOperation("从策略移除因子")
    public ResponseEntity<Void> removeFactorFromStrategy(
            @ApiParam(value = "策略ID", required = true)
            @PathVariable Long strategyId,
            @ApiParam(value = "因子ID", required = true)
            @PathVariable Long factorId) {
        strategyService.removeFactorFromStrategy(strategyId, factorId);
        return ResponseEntity.noContent().build();
    }
    
    @PutMapping("/{strategyId}/factors/{factorId}/weight")
    @ApiOperation("更新策略中因子的权重")
    public ResponseEntity<Void> updateFactorWeight(
            @ApiParam(value = "策略ID", required = true)
            @PathVariable Long strategyId,
            @ApiParam(value = "因子ID", required = true)
            @PathVariable Long factorId,
            @ApiParam(value = "权重", required = true)
            @RequestParam BigDecimal weight) {
        strategyService.updateFactorWeight(strategyId, factorId, weight);
        return ResponseEntity.noContent().build();
    }
} 